package com.camera2.dao;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;


import com.camera2.Utils.StreamUtils;
import com.camera2.activity.ActivitySplash;
import com.camera2.activity.MySetting;
import com.camera2.http.GlobalUrl;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class UpdateManager {
    private static UpdateManager manager = null;
    String Url;
    String TAG ;


    private UpdateManager() {
    }

    public static UpdateManager getInstance() {
        manager = new UpdateManager();
        return manager;
    }

    public int getVersion(Context context) {
        int version = 0;
        try {
            version = context.getPackageManager().getPackageInfo(
                    "com.lightbox.activity", 0).versionCode;
        } catch (Exception e) {
            ToMain();
        }
        return version;
    }

    public String getVersionName(Context context) {
        String versionName = null;
        try {
            versionName = context.getPackageManager().getPackageInfo(
                    "com.lightbox.activity", 0).versionName;
        } catch (Exception e) {
            ToMain();
            Log.i(TAG, "getVersionName: 获取版本名异常");

        }
        return versionName;
    }

    public String getServerVersion() {
        String serverJson = null;

        HttpURLConnection connect = null;
        Log.i(TAG, "getServerVersion: ");
        try {
            URL url = new URL(GlobalUrl.getUpdateUrl());
            connect = (HttpURLConnection) url.openConnection();
            connect.setReadTimeout(2000);
            connect.setConnectTimeout(2000);

            int responseCode = connect.getResponseCode();
            Log.i(TAG, "getServerVersion: 网络返回码.........." + responseCode);

            if (responseCode == 200) {
                InputStream is = connect.getInputStream();
                serverJson = StreamUtils.readStream(is);
                Log.i(TAG, "getServerVersion: 结果" + serverJson);
                return serverJson;
            } else {
                ToMain();
            }
        } catch (IOException e) {
            e.printStackTrace();
            new Thread() {
                public void run() {
                    Message msg = new Message();
                    msg.what = 2;
                    if (TAG.equals("ActivitySplash")) {
                        ActivitySplash.handler.sendMessage(msg);
                    }
                    if (TAG.equals("MySetting")) {
						MySetting.handler.sendMessage(msg);
                    }
                }

                ;
            }.start();

        } catch (Exception e) {
            System.out.println("执行ToMain");
            ToMain();
            Log.i(TAG, "getServerVersion:获取服务器版本号异常 " + e);

        }
        return serverJson;
    }

    public boolean CheckVersion(final Context context, String tag) {
        TAG = tag;

        new Thread() {
            public void run() {
                Looper.prepare();

                try {
                    String serverJson = manager.getServerVersion();
                    if (serverJson == null) {
                        return;
                    } else {
                        JSONArray array = new JSONArray(serverJson);
                        Log.i(TAG, "run: " + array);
                        JSONObject object = array.getJSONObject(0);
                        String Version = object.getString("version");
                        Log.i(TAG, "run: " + Version);
                        Url = object.getString("url");
                        String serverVersionName = object
                                .getString("versionName");
                        String message = object.getString("message");
                        int serverVersion = Integer.parseInt(Version);

                        int version = getVersion(context);
                        String versionName = manager.getVersionName(context);

                        if (version < serverVersion) {
                            Builder builder = new Builder(context);
                            builder.setTitle("检测到新版本,是否更新？");
                            builder.setMessage("当前版本：" + versionName + "\n\n"
                                    + "更新版本：" + serverVersionName + "\n\n"
                                    + "更新内容：" + message);
                            builder.setPositiveButton("立即更新",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(
                                                DialogInterface dialog, int arg1) {
                                            new Thread() {
                                                public void run() {
                                                    Looper.prepare();
                                                    downloadApkFile(context);
                                                    Looper.loop();
                                                }

                                                ;
                                            }.start();
                                        }
                                    });
                            builder.setNegativeButton("下次再说",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(
                                                DialogInterface dialog, int arg1) {
                                            if (TAG.equals("ActivitySplash")) {
                                                ToMain();
                                            }
                                            if (TAG.equals("MySetting")) {
                                                new Thread() {
                                                    public void run() {
                                                        Message msg = new Message();
                                                        msg.what = 3;
														MySetting.handler.sendMessage(msg);
                                                    }

                                                    ;
                                                }.start();
                                            }

                                        }
                                    });
                            builder.show();
                        } else {
                            ToMain();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    ToMain();
                    Log.i(TAG, "run: 获取服务器版本线程异常" + e);
                }
                Looper.loop();
            }

            ;
        }.start();
        return false;
    }

    public void ToMain() {
        new Thread() {
            public void run() {
                Message msg = new Message();
                msg.what = 0;
                if (TAG.equals("ActivitySplash")) {
                    ActivitySplash.handler.sendMessage(msg);
                }
                if (TAG.equals("MySetting")) {
					MySetting.handler.sendMessage(msg);
                }
            }
        }.start();
    }

    public void downloadApkFile(Context context) {
        try {
            if (Environment.MEDIA_MOUNTED.equals(Environment
                    .getExternalStorageState())) {
                URL serverURL = new URL(Url);
                HttpURLConnection connect = (HttpURLConnection) serverURL
                        .openConnection();
                BufferedInputStream bis = new BufferedInputStream(
                        connect.getInputStream());
                File apkfile = new File(GlobalUrl.getSavePath());
                BufferedOutputStream bos = new BufferedOutputStream(
                        new FileOutputStream(apkfile));

                int fileLength = connect.getContentLength();
                int downLength = 0;
                int progress = 0;
                int n;
                byte[] buffer = new byte[1024];
                while ((n = bis.read(buffer, 0, buffer.length)) != -1) {
                    bos.write(buffer, 0, n);
                    downLength += n;
                    progress = (int) (((float) downLength / fileLength) * 100);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.arg1 = progress;
                    if (TAG.equals("ActivitySplash")) {
                        ActivitySplash.handler.sendMessage(msg);
                    }
                    if (TAG.equals("MySetting")) {
						MySetting.handler.sendMessage(msg);
                    }

                }
                bis.close();
                bos.close();
                connect.disconnect();
            }

        } catch (Exception e) {
            ToMain();
            Toast.makeText(context, "下载新版本时出错，请稍候再试", Toast.LENGTH_SHORT).show();
            Log.i(TAG, "downloadApkFile:下载新版本时出错 " + e);
        }
    }
}
